import { registerDecorator, ValidationOptions } from 'class-validator';

export const ValidateStringNumber = (validationOptions?: ValidationOptions) => {
  return function (object: object, propertyName: string) {
    // 注册一个装饰器
    registerDecorator({
      // 装饰器名称
      name: 'ValidateStringNumber',
      // 装饰器目标类的构造函数
      target: object.constructor,
      // 被装饰的属性名
      propertyName: propertyName,
      // 验证选项
      options: validationOptions,
      // 验证器对象
      validator: {
        // 验证方法,检查值是否为字符串或数字类型
        validate(value: any) {
          return typeof value === 'string' || typeof value === 'number';
        },
      },
    });
  };
};
